package com.auto.code.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.auto.code.core.domain.GenFieldEntity;
import com.auto.code.core.domain.GenTableEntity;
import com.auto.code.core.enums.GenFieldType;
import com.auto.code.core.enums.TypeConvertEnum;
import com.auto.code.core.enums.TypeXmlEnum;

public class GenCodeUtil {
	private static String half_tab = "  ";
	private static String tab = "    ";
	private static String two_tab = "    	";
	private static String three_tab = "    			";
	private static String four_tab = "    				";
	private static Pattern linePattern = Pattern.compile("_(\\w)");
	
	/**
	 * 获取数据库表的所有字段
	 * @param fields
	 * @return
	 */
	public static String getGetSetFiled(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for(GenFieldEntity field : fields) {
			String fieldName = field.getFieldName();
			if(StringUtils.isEmpty(field.getChinaName())){
				sb.append(tab + "/******/" + "\n");
			}else{
				sb.append(tab + "/***" + field.getChinaName() + "***/" + "\n");
			}
			sb.append(tab + "@ApiModelProperty(value = \"" + field.getChinaName() + "\")\n");
			sb.append(tab + "private " + TypeConvertEnum.getTypeByDb(field.getFieldType()) + " " + getObjName(fieldName) + ";" + "\n\n");
		}
		return sb.toString();
	}
	
	/**\"
	 * 获取数据库表的所有字段 set get
	 * @param fields
	 * @return
	 */
	public static String getGetSetMethod(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for(GenFieldEntity field : fields) {
			
			String fieldName = field.getFieldName();
			
			sb.append("\n" + tab + "public " + TypeConvertEnum.getTypeByDb(field.getFieldType()) + " get" + lineToHump(fieldName) + "() {" + "\n");
			sb.append(tab + tab + "return " + getObjName(fieldName) + ";\n");
			sb.append(tab + "}\n");
					
			sb.append("\n" + tab + "public void set" + lineToHump(fieldName) + "(" + TypeConvertEnum.getTypeByDb(field.getFieldType()) + " " + getObjName(fieldName) + ") {" + "\n");	
			sb.append(tab + tab + "this." + getObjName(fieldName) + " = " + getObjName(fieldName) + ";\n");
			sb.append(tab + "}\n");
		}
		return sb.toString();
	}
	
	/**
	 * 获取数据库表的所有字段 驼峰 
	 * @return
	 */
	public static String lineToHump(String name) {
		String temp = getObjName(name);
		char[] chars = temp.toCharArray();
		chars[0] = (char)(chars[0] - 32);
		return String.valueOf(chars);
	}
	
	/**
	 * 获取数据库表的所有字段驼峰 首字母小写
	 * @return
	 */
	public static String getObjName(String name) {
		name = name.replaceAll("-", "_").toLowerCase();
		Matcher matcher = linePattern.matcher(name);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	
	public static void main(String[] args) {
		List<GenFieldEntity> fields = new ArrayList<GenFieldEntity>();
		GenFieldEntity a = new GenFieldEntity();
		a.setFieldType("BIGINT");
		a.setFieldName("id");
		a.setComment("姓名");

		GenFieldEntity b = new GenFieldEntity();
		b.setFieldType("VARCHAR");
		b.setFieldName("spec_name");
		b.setComment("年龄");

		GenFieldEntity c = new GenFieldEntity();
		c.setFieldType("VARCHAR");
		c.setFieldName("enter_item_log");
		c.setComment("姓名");

		fields.add(a);
		fields.add(b);
//		System.out.println(getGetSetFiled(fields));
//		System.out.println(getGetSetMethod(fields));

//		System.out.println(lineToHump("enter_item_log"));

//		System.out.println(getSqlCondition(fields));
		System.out.println(getJsTableFiledVal(fields));
//		System.out.println("dasdasdasd".replaceAll("da", "var specName = \\$(\"#specName\").val();"));
//		System.out.println("dasdasdasd".replaceAll("da", getJsTableFiledVal(fields)));
	}

	public static void testReplace(){
		String a = "com.mkb";

		System.out.println(a.replaceAll("\\.", "\\/"));;
	}

	public static void testReplace2(){
		String a = "${table_class_name}DTO.java";
		String temp = a.replaceAll("\\$\\{table_class_name\\}", "mkb");
		System.out.println(temp);
	}

	public static void testReplace3(){
		String a = "List&lt;String&gt;";
		String temp = a.replaceAll("&gt;", ">").replaceAll("&lt;", "<");
		System.out.println(temp);
	}

	/**
	 * 获取sql返回的map
	 * @param fields
	 * @return
	 */
	public static String getSqlMapClumn(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for(GenFieldEntity field : fields){
			if(field.getFieldName().equalsIgnoreCase("id")){
				sb.append(tab + "<id column=\"id\" jdbcType=\"BIGINT\" property=\"id\" />"  + "\n");
			}else{
				sb.append(tab + "<result column=\"" + field.getFieldName() + "\" jdbcType=\"" + TypeXmlEnum.getTypeByDb(field.getFieldType()) + "\" property=\"" + getObjName(field.getFieldName()) + "\" />" + "\n");
			}
		}
		return sb.toString();
	}

	/**
	 * 获取表的所有列
	 * @param fields
	 * @return
	 */
	public static String getSqlColumn(List<GenFieldEntity> fields) {
		List<String> fieldList = new ArrayList<>();
		for(GenFieldEntity entity : fields){
			fieldList.add(entity.getFieldName());
		}
		return StringUtils.join(fieldList, ", ");
	}

	/**
	 * 获取  封装更新字段
	 * @param fields
	 * @return
	 */
	public static String getUpdateFiled(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for(GenFieldEntity field : fields){
			sb.append(two_tab + "<if test = \"" + getObjName(field.getFieldName()) + " != null");
			if(GenFieldType.STRING.getType().equalsIgnoreCase(TypeConvertEnum.getTypeByDb(field.getFieldType()))){
				sb.append(" and " + getObjName(field.getFieldName()) + " != '' ");
			}
			sb.append("\" >" + "\n");

			sb.append(three_tab + field.getFieldName() + "=" +"#{" + getObjName(field.getFieldName()) + ",jdbcType=" + TypeXmlEnum.getTypeByDb(field.getFieldType()) + "}," + "\n");

			sb.append(two_tab + "</if>" + "\n");
		}
		return  sb.toString();
	}

	/**
	 * 获取列的对应的java属性
	 * @param fields
	 * @return
	 */
	public static String getSqlColumnAttr(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		List<String> fieldList = new ArrayList<>();
		for(GenFieldEntity field : fields){
			fieldList.add("#{" + getObjName(field.getFieldName()) + ",jdbcType=" + TypeXmlEnum.getTypeByDb(field.getFieldType()) + "}");
		}
		return StringUtils.join(fieldList, ", ");
	}

	/**
	 * 获取sql查询条件
	 * @param fields
	 * @return
	 */
	public static String getSqlCondition(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		sb.append(tab + "<where>" + "\n");
		for(GenFieldEntity field : fields){

			sb.append(two_tab + "<if test=\"" + getObjName(field.getFieldName()) + " != null");
			if(GenFieldType.STRING.getType().equalsIgnoreCase(TypeConvertEnum.getTypeByDb(field.getFieldType()))){
				sb.append(" and " + getObjName(field.getFieldName()) + " != '' ");
			}
			sb.append("\" >" + "\n");
			sb.append(three_tab + "and " + field.getFieldName() + "=" +"#{" + getObjName(field.getFieldName()) + ",jdbcType=" + TypeXmlEnum.getTypeByDb(field.getFieldType()) + "}" + "\n");

			sb.append(two_tab + "</if>" + "\n");
		}
		sb.append(tab + "</where>" + "\n");
		return  sb.toString();
	}

	public static String getModulURL(List<GenTableEntity> dbTables) {
		StringBuilder sb = new StringBuilder();
		for(GenTableEntity tableEntity : dbTables){
			sb.append(tab + "String " + tableEntity.getTableName().toUpperCase() + " = BASE_PATH + " + "\"/" + getObjName(tableEntity.getTableName()) + "\";" + "\n");
		}
		return sb.toString();
	}


	public static String getJsTableFiledVal(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for(GenFieldEntity field : fields){
			sb.append(two_tab + "var " + getObjName(field.getFieldName()) + " = \\$(\"#" + getObjName(field.getFieldName()) + "\").val();" + "\n");
		}
		return sb.toString();
	}


	public static String getSendJsTableFiledVal(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < fields.size(); i++) {
			if(i != fields.size() - 1){
				sb.append(three_tab + getObjName(fields.get(i).getFieldName()) + " : " + getObjName(fields.get(i).getFieldName()) +  "," + "\n");
			}else{
				sb.append(three_tab + getObjName(fields.get(i-1).getFieldName()) + " : " + getObjName(fields.get(i-1).getFieldName()));
			}
		}
		return sb.toString();
	}

	public static String getTableFTL(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= fields.size(); i++) {
			if(i != fields.size()){
				sb.append(tab + getObjName(fields.get(i).getFieldName()) + " : " + getObjName(fields.get(i).getFieldName()) +  "," + "\n");
			}else{
				sb.append(tab + getObjName(fields.get(i-1).getFieldName()) + " : " + getObjName(fields.get(i-1).getFieldName()));
			}
		}
		return sb.toString();
	}

	public static String getTdFiled(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for(GenFieldEntity field : fields){
			sb.append(three_tab + "<td>" + "\\${item." + getObjName(field.getFieldName()) + "!''}" + "</td>"  + "\n");
		}
		return sb.toString();
	}

	public static String getThFiled(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		for(GenFieldEntity field : fields){
			sb.append(two_tab + "<th width=\"5%\">" + field.getChinaName() + "</th>"  + "\n");
		}
		return sb.toString();
	}

	public static String getSqlColumnAttr2(List<GenFieldEntity> fields) {
		StringBuilder sb = new StringBuilder();
		List<String> fieldList = new ArrayList<>();
		for(GenFieldEntity field : fields){
			fieldList.add("#{obj." + getObjName(field.getFieldName()) + ",jdbcType=" + TypeXmlEnum.getTypeByDb(field.getFieldType()) + "}");
		}
		return StringUtils.join(fieldList, ", ");
	}
}
